<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Organization\Repository;

use App\Repository\IRepository;
use Hyperf\Collection\Arr;
use Hyperf\Database\Model\Builder;
use Plugin\Organization\Model\RdOrganizeUserStand;

/**
 * 用户管理站点 Repository类
 */
class RdOrganizeUserStandRepository extends IRepository
{
    public function __construct(
        protected readonly RdOrganizeUserStand $model
    )
    {
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {

        // 用户ID
        if (isset($params['user_id']) && filled($params['user_id'])) {
            $query->where('user_id', '=', $params['user_id']);
        }

        // 所属工区ID
        if (Arr::get($params, 'stand_id')) {
            $query->whereIn('stand_id', Arr::wrap($params['stand_id']));
        }

        // 角色
        if (Arr::get($params, 'role')) {
            $query->whereIn('role', Arr::wrap($params['role']));
        }

        // 状态
        if (isset($params['status']) && filled($params['status'])) {
            $query->where('status', '=', $params['status']);
        }

        // 
        if (isset($params['created_at']) && filled($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [$params['created_at'][0], $params['created_at'][1]]
            );
        }

        // 
        if (isset($params['updated_at']) && filled($params['updated_at']) && is_array($params['updated_at']) && count($params['updated_at']) == 2) {
            $query->whereBetween(
                'updated_at',
                [$params['updated_at'][0], $params['updated_at'][1]]
            );
        }

        // 备注
        if (isset($params['remark']) && filled($params['remark'])) {
            $query->where('remark', 'like', '%' . $params['remark'] . '%');
        }

        return $query;
    }

    public function page(array $params = [], ?int $page = null, ?int $pageSize = null): array
    {
        $result = $this->perQuery($this->getQuery(), $params)
            ->with(['user:id,username,phone', 'stand:id,name,area_id,proj_line_id,zone_id', 'stand.area:id,name', 'stand.projLine:id,name', 'stand.zone:id,name'])
            ->paginate(
                perPage: $pageSize,
                pageName: static::PER_PAGE_PARAM_NAME,
                page: $page,
            );
        return $this->handlePage($result);
    }

    protected function enablePageOrderBy(): bool
    {
        return false;
    }

    public function getStandUsers($stand, $role = 0)
    {
        $query = $this->getQuery()->where('stand_id', $stand)->where('status', 1);
        if ($role != 0) {
            return $query->where('role', $role)->pluck('user_id')->toArray();
        }
        return $query->pluck('user_id')->toArray();
    }

    public function getStandRole($standId, $userId)
    {
        return $this->getQuery()->where('user_id', $userId)
            ->where('status', 1)
            ->where('stand_id', $standId)
            ->pluck('role')->first() ?? 0;
    }

    public function getAdminStands($userId)
    {
        return $this->getQuery()->where('user_id', $userId)
            ->where('status', 1)
            ->where('role', 1)
            ->pluck('stand_id')->toArray();
    }
}